class DatafileController < ApplicationController
  
  
  def make_property_analysis_file
    
    div = {
  'CES'             =>  'CES',
  'Ops'             =>  'UKO',
  'Retail'          =>  'RET',
  'Ops/CT'          =>  'UCT',
  'Ops/Retail'      =>  'URE',
  'Ops/CT/Terr'     =>  'UTT',
  'CT'              =>  'CT',
  'Ops/CT/Options'  =>  'UCO',
  'Ops/CT/Terr/UKO' =>  'UTT'
    }
    
    who = {
  'GM'      => 'GM',
  'AN'      => 'BT',
  'MD'      => 'MD',
  'SR/GP'   => 'GP',
  'GP'      => 'GP',
  'MJ'      => 'MJ',
  'GA'      => 'GA',
  'AN/CH'   => 'AN',
  'DE'      => 'DE',
  'CH'      => 'CH',
  'GP/AW'   => 'GP',
  'GP/AN'   => 'GP',
  'ALN'     => 'AN', 
  'MC'      => 'AN'
    }
    
    typ = {
  'A'      => 'A', 
  'D'      => 'D', 
  'C'      => 'C', 
  'D/A'    => 'D',
  'A/C'    => 'A', 
  'Sublet'    => 'D',  
    }
    
    bus = {
  'Draft'       => 'AW',
  'N/A'         => 'N/A', 
  'Y'         => 'YES', 
  'N'          => 'NO', 
  'Awaited'     => 'AW', 
  'Approved'    => 'YES'
    }
    
    otg = {
    'Draft'     => 'N',
    'Y'         => 'Y',
    'N'         => 'N',
    '2007'      => 'N',
    '2008'      => 'N',
    '2009'      => 'N',
    '2010'      => 'N',
    'Undecided' => 'N'
    }
    
    yoc = {
    2007         => '07',
    2008         => '08',
    2009         => '09',
    }
    
    yoq = {
    'Q1'    => 'Q1',
    'Q2'    => 'Q2', 
    'Q3'    => 'Q3', 
    'Q4'    => 'Q4', 
    '2008'  => '08', 
    '2009'  => '09', 
    '2010'  => '10'
    }
    
    cmt = {
    'Y'   => 'Y',
    'N'   => 'N'
    }
    
    
    #for each property id add property code tab analysis group code tab analysis group type code new line to content file
    string=String.new
    #For Division (DIV) /Business Unit
    Bid.find(:all).each { |bid| string << bid.prop_ref.to_s+"\t"+"DIV"+"\t"+div[bid.business_unit]+"\n"  }
    #For Property Dept Contact (WHO)
    Bid.find(:all).each { |bid| string << bid.prop_ref.to_s+"\t"+"WHO"+"\t"+who[bid.prop_dept_contact]+"\n"  }
    #For Project Type (TYP)
    Bid.find(:all).each { |bid| string << bid.prop_ref.to_s+"\t"+"TYP"+"\t"+typ[bid.project_type]+"\n"  }
    #For Business Case (BUS)
    Bid.find(:all).each { |bid| string << bid.prop_ref.to_s+"\t"+"BUS"+"\t"+bus[bid.business_case]+"\n"  }
    #For Year (YOC)
    Bid.find(:all).each { |bid| string << bid.prop_ref.to_s+"\t"+"YOC"+"\t"+yoc[bid.year]+"\n"  }
    #For On_target *(OTG) Needs to be set up on Propman
    Bid.find(:all).each { |bid| string << bid.prop_ref.to_s+"\t"+"OTG"+"\t"+otg[bid.on_target]+"\n"  }
    #For Cashflow (YOQ)
    Bid.find(:all).each { |bid| string << bid.prop_ref.to_s+"\t"+"YOQ"+"\t"+yoq[bid.cashflow_qtr]+"\n"  }
    #For Completed * (CMT)Needs to be set up on Propman
    Bid.find(:all).each { |bid| string << bid.prop_ref.to_s+"\t"+"CMT"+"\t"+cmt[bid.completed]+"\n"  }
    
    File.open("/mnt/propman/import/data/anltyp-P.txt", "w")  { |f| f.write(string) }
    
    @pa_content = string 
    @pa_content_type='text/plain'
    @response.headers["Content-Type"] = @pa_content_type
    render :layout=>false
    
    
  end
  
  def make_key_note_file
    string=String.new
    #for RM2005 keynote is property id tab1 key-word tab2 tab3 tab4 tab5 tab6 tab7 tab8 tab9 cap_receipt.to_s tab10 cap_spend.to_s tab11 tab12 cap_retail_spend.to_s tab13 new_line
    Bid.find(:all).each { |bid| string << bid.prop_ref.to_s+"\t"+"RM2005"+"\t\t\t\t\t\t\t\t"+bid.cap_receipt.to_s+"\t"+bid.cap_spend.to_s+"\t\t"+bid.cap_retail_spend.to_s+"\t"+"\n"  }
    #for Top20 keynote is property id tab1 key-word tab2 tab3 tab4 tab5 tab6 tab7 tab8 tab9 tab10 tab11 tab12 tab13 comments new-line
    Bid.find(:all).each { |bid| string << bid.prop_ref.to_s+"\t"+"Top20"+"\t\t\t\t\t\t\t\t\t\t\t\t"+bid.comments+"\n"  }
    
    File.open("/mnt/propman/import/data/pkeynote.txt", "w")  { |f| f.write(string) }
    
    @kn_content = string 
    @kn_content_type='text/plain'
    @response.headers["Content-Type"] = @kn_content_type
    render :layout=>false
    
  end
  
  
  
end
